/******************************************************************************
* Copyright (C) 2020 ShenZhen Powerdata Information Technology Co.,Ltd
* All Rights Reserved.
* 本软件为***研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
* 复制、修改或发布本软件.
*****************************************************************************/

package com.xgf.common.xgfcommon;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.xgf.common.xgfcommon.utils.DateUtils;
import com.xgf.common.xgfcommon.utils.StringUtils;

/**
* @Title:
* @ClassName:  A   
* @Description:TODO  
* @author: xgf
* @date:   2020年2月18日 下午1:57:55   
* @since   JDK1.8
* @history 2020年2月18日 TODO 新建
*/
public class A {
	
	public static void main(String[] args) throws ParseException {
		HashMap<String, List<String>> result = new HashMap<>();
		try {
			ArrayList<Object> arrayList = new ArrayList<>();
			
			File file = new File("D:\\b\\tms_log.txt");
			InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file));
			BufferedReader bf = new BufferedReader(inputReader);
			// 按行读取字符串
			String str;
			while ((str = bf.readLine()) != null) {
				//获取登录用户 和 时间
				arrayList.add(str);
				getUserNameAndDate(str, result);
			}
			bf.close();
			inputReader.close();
			
		} catch (IOException e) {
			e.printStackTrace();
	}finally {
	}
		
		//重排序 输出用户名 然后没有就输出无
//		String[] s = {"sujimin","zhangxue","liying","zhugeyugang","hanliwei","zhuxiaofei","gaolianyi","hezheng","zhangxue01","zhangping","xiedan","guolulu","zhangrui","liyang","liushuai","yannan","gaomei","zhanghuan","chenhuan","gouchunyan","Lily","weiqiuyun","liuyang","wufeng","yanchen","wangyue","liangmengxi","cairuixue","hedan","liangmengxi","lihui01","lili","yinyun","dongling","shilijun","yangyifei","wangshuang","xingshuo"};
		String[] s2 = {"jiabin",
				"hezheng",
				"wangyue",
				"liushuai",
				"lihui",
				"zhangping",
				"shilijun",
				"liying01",
				"Ly",
				"yannan",
				"liujiamei",
				"pengzhu",
				"liyang",
				"guolulu",
				"sujimin",
				"chenhuan",
				"zhangrui",
				"wufeng1",
				"chenhuan01",
				"gaolianyi",
				"dongguanglei",
				"xinshuo",
				"wangshuang",
				"xingshuo",
				"liangmengxi",
				"gaomei",
				"yangyifei",
				"liuhuan",
				"weiqiuyun",
				"lili",
				"yinyun",
				"xiedan",
				"zhangxue1",
				"zhanghuan",
				"dongling",
				"zhangmengning",
				"wanghui",
				"yanchen01",
				"zhangxue",
				"cairuixue",
				"liying",
				"zhangtianhao",
				"hedan",
				"liuyang1",
				"lihui01",
				"gouchunyan"
};
		SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Date startTime = simpleDateFormat2.parse("2020-02-08 00:00:00");
		 DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < 10; i++) {
			List<String> start = start(s2, result, startTime, i);
			List<String> end = end(s2, result, startTime, i);
			System.out.println(start);
			Date addDay = DateUtils.addDay(startTime, i);
			String path = "D:\\b\\tms_login\\" + f.format(addDay);
			StringBuilder stringBuilder = new StringBuilder();
			List<String> arrayList = new ArrayList<>(100);
			arrayList.add("--------------最开始的登录时间-------------------------------");
			arrayList.addAll(start);
			arrayList.add("------------最后的登录时间-------------------------------");
			arrayList.addAll(end);
			try {
				com.xgf.common.xgfcommon.utils.FileUtils.writeStringToFile(path, arrayList);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			
		}
		

	}
	
	//获取最开始的时间
	public static List<String> start(String[] s2 , HashMap<String, List<String>> result, Date startTime, int addDay) {
		
		List<String> arrayList = new LinkedList<>();
		for (int i = 0; i < s2.length; i++) {
			String username = s2[i];
			List<String> list = result.get(username);
			if(list == null) {
				//换行
				arrayList.add("");
				continue;
			}
			List<String> dateListByBetweenTime = DateUtils.getDateListByBetweenTime(list, startTime, addDay);
			if(dateListByBetweenTime == null ||dateListByBetweenTime.size() <= 0) {
				arrayList.add("");
			}
			Collections.sort(dateListByBetweenTime, new Comparator<String>() {
			    DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			    @Override
			    public int compare(String o1, String o2) {
			        try {
			               return f.parse(o1).compareTo(f.parse(o2));
			        } catch (ParseException e) {
			               throw new IllegalArgumentException(e);
			        }
			    }
			});
			
			for (String string : dateListByBetweenTime) {
				arrayList.add(string);
				break;
			}
			
		}
		return arrayList;
	}
	
	
	//获取最结尾的时间
	public static List<String> end(String[] s2 , HashMap<String, List<String>> result, Date startTime, int addDay) {
		
		List<String> arrayList = new LinkedList<>();
		
		for (int i = 0; i < s2.length; i++) {
			String username = s2[i];
			List<String> list = result.get(username);
			if(list == null) {
				//换行
				arrayList.add("");
				continue;
			}
			List<String> dateListByBetweenTime = DateUtils.getDateListByBetweenTime(list, startTime, addDay);
			if(dateListByBetweenTime == null ||dateListByBetweenTime.size() <= 0) {
				arrayList.add("");
				continue;
			}
			Collections.sort(dateListByBetweenTime, new Comparator<String>() {
			    DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			    @Override
			    public int compare(String o1, String o2) {
			        try {
			               return f.parse(o1).compareTo(f.parse(o2));
			        } catch (ParseException e) {
			               throw new IllegalArgumentException(e);
			        }
			    }
			});
			arrayList.add(dateListByBetweenTime.get(dateListByBetweenTime.size() -1 ));
//			stringBuilder.append(dateListByBetweenTime.get(dateListByBetweenTime.size())).append("/r/n");
			
		}
		return arrayList;
	}
	/**
	 * 获取时间 和 用户名
	* @Title: a
	* @Description: TODO
	* @param  
	* @return void
	* @throws
	 */
	public static void getUserNameAndDate(String content, HashMap<String, List<String>> result) {
		if(content == null ||content.length() <= 0) {
			return;
		}
		int characterPosition = com.xgf.common.xgfcommon.utils.StringUtils.getCharacterPosition(content, 2, "\"");
		String username = content.substring(content.indexOf("登录用户：") + 5, characterPosition);
		String datetime = content.substring(com.xgf.common.xgfcommon.utils.StringUtils.getCharacterPosition(content, 3, "\"") + 1, content.lastIndexOf("\""));
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date parse = simpleDateFormat.parse(datetime);
			String format = simpleDateFormat2.format(parse);
			System.out.println(username + "   "  +format );
			List<String> list = result.get(username);
			if(list == null || list.size() <= 0) {
				list = new ArrayList<>();
				list.add(format);
				
			}else {
				list.add(format);
			}
			
			result.put(username, list);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
		
	}
}
